{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6e5de856-5e7d-40f1-93fe-419bd7564075",
   "metadata": {},
   "source": [
    "# pandas 数据分析库\n",
    "\n",
    "```sh\n",
    "#安装 pandas\n",
    "pip install pandas\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3fd7db19-3c7a-40bb-9502-1c9175bd2276",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09224cc6-eb07-4e4a-b30a-50ddbf44c38b",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## 数据结构"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77f451af-58ae-4a3b-bfbe-421a74bfa4a2",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### 一维 Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c2ab60e0-7868-40ea-82f0-6e58b43c38bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "d    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Series(data = [1, 2, 3, 4], index = ['a', 'b', 'c', 'd'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8e1773c6-4400-485e-b836-9a74dd4b1264",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    2\n",
       "1    4\n",
       "2    5\n",
       "3    9\n",
       "dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 不指定索引，使用默认索引，从 0 开始\n",
    "pd.Series(data = [2, 4, 5, 9])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "996279fc-27dd-4ef2-b59d-db4bb5715ff7",
   "metadata": {},
   "source": [
    "### 二维 DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c3d1eec1-cb42-4087-8fe1-4c047fc0d0c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>English</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>44.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>2.0</td>\n",
       "      <td>33.0</td>\n",
       "      <td>29.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>29.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>62.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>81.0</td>\n",
       "      <td>61.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>50.0</td>\n",
       "      <td>51.0</td>\n",
       "      <td>80.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  English  Math\n",
       "A    44.0     84.0   4.0\n",
       "B     2.0     33.0  29.0\n",
       "C    29.0     24.0  62.0\n",
       "D    81.0     61.0   1.0\n",
       "E    50.0     51.0  80.0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(data = np.random.randint(0, 100, size = (5, 3)),\n",
    "            columns=['Python', 'English', 'Math'],\n",
    "            index=list('ABCDE'),\n",
    "            dtype=np.float32)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8e71c52-240c-4b97-b06b-7b9ea9bd9a5e",
   "metadata": {},
   "source": [
    "**另一种创建方式**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c9edb707-9b18-4d20-bdd4-3128a0b21a5b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>English</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>23.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>59.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>35.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>29.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>40.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>33.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>30.0</td>\n",
       "      <td>71.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>11.0</td>\n",
       "      <td>69.0</td>\n",
       "      <td>67.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  English  Math\n",
       "A    23.0     11.0  59.0\n",
       "B    35.0     52.0  29.0\n",
       "C    40.0     10.0  33.0\n",
       "D    30.0     71.0  30.0\n",
       "E    11.0     69.0  67.0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(data = {\n",
    "    'Python': np.random.randint(0, 100, size=5),\n",
    "    'English': np.random.randint(0, 100, size=5),\n",
    "    'Math': np.random.randint(0, 100, size=5),\n",
    "}, index = list('ABCDE'), dtype = np.float32)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "942ef490-63c4-4ae8-9946-21d1be034e82",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## 数据查看"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d84a4425-1139-4a36-b852-702207f3c504",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(data = np.random.randint(0, 100, size = (150, 3)),\n",
    "            columns=['Python', 'English', 'Math'],\n",
    "            dtype=np.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "89bf7f2e-ffe1-43fd-8c02-638dbafb0d42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>English</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>32.0</td>\n",
       "      <td>91.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>95.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>77.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>99.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>78.0</td>\n",
       "      <td>86.0</td>\n",
       "      <td>98.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>57.0</td>\n",
       "      <td>75.0</td>\n",
       "      <td>18.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>33.0</td>\n",
       "      <td>49.0</td>\n",
       "      <td>76.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>17.0</td>\n",
       "      <td>44.0</td>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>10.0</td>\n",
       "      <td>46.0</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>48.0</td>\n",
       "      <td>51.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>74.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>64.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Python  English  Math\n",
       "0      32.0     91.0   5.0\n",
       "1      95.0     15.0  77.0\n",
       "2      99.0     13.0  20.0\n",
       "3      78.0     86.0  98.0\n",
       "4      57.0     75.0  18.0\n",
       "..      ...      ...   ...\n",
       "145    33.0     49.0  76.0\n",
       "146    17.0     44.0  50.0\n",
       "147    10.0     46.0  12.0\n",
       "148    48.0     51.0   3.0\n",
       "149    74.0     17.0  64.0\n",
       "\n",
       "[150 rows x 3 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "411a0aa1-c9cd-4e8d-8727-695be6bac218",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(150, 3)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看形状\n",
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d715be25-5265-4860-990e-ad6040f532c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>English</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>32.0</td>\n",
       "      <td>91.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>95.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>77.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>99.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>78.0</td>\n",
       "      <td>86.0</td>\n",
       "      <td>98.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>57.0</td>\n",
       "      <td>75.0</td>\n",
       "      <td>18.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>32.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>58.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>23.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>26.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>59.0</td>\n",
       "      <td>57.0</td>\n",
       "      <td>56.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>16.0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>55.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>69.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>44.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  English  Math\n",
       "0    32.0     91.0   5.0\n",
       "1    95.0     15.0  77.0\n",
       "2    99.0     13.0  20.0\n",
       "3    78.0     86.0  98.0\n",
       "4    57.0     75.0  18.0\n",
       "5    32.0     16.0  58.0\n",
       "6    23.0     30.0  26.0\n",
       "7    59.0     57.0  56.0\n",
       "8    16.0     35.0  55.0\n",
       "9    69.0     16.0  44.0"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#显示前 10 个，如果不指定数字，默认为 5 个\n",
    "df.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "480efebf-191f-44c4-b33d-51142d9ecb7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>English</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>33.0</td>\n",
       "      <td>49.0</td>\n",
       "      <td>76.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>17.0</td>\n",
       "      <td>44.0</td>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>10.0</td>\n",
       "      <td>46.0</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>48.0</td>\n",
       "      <td>51.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>74.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>64.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     Python  English  Math\n",
       "145    33.0     49.0  76.0\n",
       "146    17.0     44.0  50.0\n",
       "147    10.0     46.0  12.0\n",
       "148    48.0     51.0   3.0\n",
       "149    74.0     17.0  64.0"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 默认查看后 5 个，可以指定数字\n",
    "df.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4d69d14e-7cd6-4f66-b2ee-c9db95671b7e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Python     float32\n",
       "English    float32\n",
       "Math       float32\n",
       "dtype: object"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "48dff346-2c3e-4469-a4e6-540740a39be5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Python       int64\n",
       "English    float32\n",
       "Math       float32\n",
       "dtype: object"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#修改 Python 列类型\n",
    "df['Python'] = df['Python'].astype(np.int64)\n",
    "\n",
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ae7e14b4-21e3-450b-a273-44f9d141e681",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=150, step=1)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取行索引\n",
    "df.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "82895f53-5726-49b9-8062-8af26a30ac5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Python', 'English', 'Math'], dtype='object')"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取列索引\n",
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "11ac915f-d075-46b3-8db0-32bee1987e01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看 df 中的 ndarray。这里可以推断 pandas 是基于 numpy 开发的\n",
    "type(df.values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b2170bab-fd2a-435b-8d6c-25466800634f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(150, 3)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.values.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "919ce77c-d7a1-499e-af27-71c5ae95214d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>English</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>150.000000</td>\n",
       "      <td>150.000000</td>\n",
       "      <td>150.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>52.233333</td>\n",
       "      <td>49.720001</td>\n",
       "      <td>50.586666</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>28.160236</td>\n",
       "      <td>28.377163</td>\n",
       "      <td>29.274719</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>30.250000</td>\n",
       "      <td>25.500000</td>\n",
       "      <td>23.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>50.500000</td>\n",
       "      <td>49.000000</td>\n",
       "      <td>51.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>76.750000</td>\n",
       "      <td>75.000000</td>\n",
       "      <td>75.750000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>99.000000</td>\n",
       "      <td>99.000000</td>\n",
       "      <td>99.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           Python     English        Math\n",
       "count  150.000000  150.000000  150.000000\n",
       "mean    52.233333   49.720001   50.586666\n",
       "std     28.160236   28.377163   29.274719\n",
       "min      1.000000    1.000000    1.000000\n",
       "25%     30.250000   25.500000   23.000000\n",
       "50%     50.500000   49.000000   51.500000\n",
       "75%     76.750000   75.000000   75.750000\n",
       "max     99.000000   99.000000   99.000000"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看数值型列的统计信息，属性有 计数、平均值、标准差、最小值、四分位数、中位数、最大值\n",
    "df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "4cb0e7f8-3f6b-4aca-873f-e2943762c07c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 150 entries, 0 to 149\n",
      "Data columns (total 3 columns):\n",
      " #   Column   Non-Null Count  Dtype  \n",
      "---  ------   --------------  -----  \n",
      " 0   Python   150 non-null    int64  \n",
      " 1   English  150 non-null    float32\n",
      " 2   Math     150 non-null    float32\n",
      "dtypes: float32(2), int64(1)\n",
      "memory usage: 2.5 KB\n"
     ]
    }
   ],
   "source": [
    "#查看列索引、数据类型、非空计数、内存信息\n",
    "df.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a9d5248-2e5c-42d7-bbc5-6f762d8b5410",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## 数据加载与持久化"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61725aa8-5cf0-4f82-a120-85f00acbc817",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### csv 格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ebe75cf2-c3da-4d79-a825-9e4e0bbfe691",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>IT</th>\n",
       "      <th>化工</th>\n",
       "      <th>生物</th>\n",
       "      <th>教师</th>\n",
       "      <th>土木</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>37</td>\n",
       "      <td>38</td>\n",
       "      <td>21</td>\n",
       "      <td>25</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>46</td>\n",
       "      <td>10</td>\n",
       "      <td>30</td>\n",
       "      <td>32</td>\n",
       "      <td>49</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7</td>\n",
       "      <td>40</td>\n",
       "      <td>16</td>\n",
       "      <td>36</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>39</td>\n",
       "      <td>45</td>\n",
       "      <td>29</td>\n",
       "      <td>39</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>39</td>\n",
       "      <td>16</td>\n",
       "      <td>31</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   IT  化工  生物  教师  土木\n",
       "0  37  38  21  25  18\n",
       "1  46  10  30  32  49\n",
       "2   7  40  16  36  29\n",
       "3  39  45  29  39   3\n",
       "4   2  39  16  31  47"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(data = np.random.randint(0, 50, size = (50, 5)),\n",
    "               columns = ['IT', '化工', '生物', '教师', '土木'])\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "cd2c4e00-f21b-40c9-ac7b-8c35433c4a8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 持久化到 csv 文件\n",
    "#   sep: 分隔符，默认为英文逗号\n",
    "#   header: 是否保存列索引\n",
    "#   index: 是否保存行索引。文件被加载时，默认的行索引会被作为一列\n",
    "df.to_csv('salary.csv', sep=':', header=True, index=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f37b4de-8cdb-4faa-8715-5d67e0cc98de",
   "metadata": {},
   "source": [
    "salary.csv 前几行数据：\n",
    "```\n",
    ":IT:化工:生物:教师:土木\n",
    "0:30:38:23:48:10\n",
    "1:10:31:3:37:11\n",
    "2:16:41:28:26:42\n",
    "3:36:39:1:47:34\n",
    "4:14:38:8:2:7\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1931975e-7800-4aa6-b7a3-4c4c2f4fa899",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>IT</th>\n",
       "      <th>化工</th>\n",
       "      <th>生物</th>\n",
       "      <th>教师</th>\n",
       "      <th>土木</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>37</td>\n",
       "      <td>38</td>\n",
       "      <td>21</td>\n",
       "      <td>25</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>46</td>\n",
       "      <td>10</td>\n",
       "      <td>30</td>\n",
       "      <td>32</td>\n",
       "      <td>49</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7</td>\n",
       "      <td>40</td>\n",
       "      <td>16</td>\n",
       "      <td>36</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>39</td>\n",
       "      <td>45</td>\n",
       "      <td>29</td>\n",
       "      <td>39</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>39</td>\n",
       "      <td>16</td>\n",
       "      <td>31</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   IT  化工  生物  教师  土木\n",
       "0  37  38  21  25  18\n",
       "1  46  10  30  32  49\n",
       "2   7  40  16  36  29\n",
       "3  39  45  29  39   3\n",
       "4   2  39  16  31  47"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取 csv 文件\n",
    "#   sep: 指定分隔符\n",
    "#   header: 指定列索引\n",
    "#   index_col: 指定行索引\n",
    "df2 = pd.read_csv('salary.csv', sep=':', header=[0], index_col=0)\n",
    "df2.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d338dc0-ea6d-4ce7-9110-7b1712b93025",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### excel 格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a081fdc8-7c98-4869-8296-b4914e4469e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>IT</th>\n",
       "      <th>化工</th>\n",
       "      <th>生物</th>\n",
       "      <th>教师</th>\n",
       "      <th>土木</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>37</td>\n",
       "      <td>38</td>\n",
       "      <td>21</td>\n",
       "      <td>25</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>46</td>\n",
       "      <td>10</td>\n",
       "      <td>30</td>\n",
       "      <td>32</td>\n",
       "      <td>49</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7</td>\n",
       "      <td>40</td>\n",
       "      <td>16</td>\n",
       "      <td>36</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>39</td>\n",
       "      <td>45</td>\n",
       "      <td>29</td>\n",
       "      <td>39</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>39</td>\n",
       "      <td>16</td>\n",
       "      <td>31</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   IT  化工  生物  教师  土木\n",
       "0  37  38  21  25  18\n",
       "1  46  10  30  32  49\n",
       "2   7  40  16  36  29\n",
       "3  39  45  29  39   3\n",
       "4   2  39  16  31  47"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = pd.DataFrame(data = np.random.randint(0, 50, size = (50, 5)),\n",
    "               columns = ['IT', '化工', '生物', '教师', '土木'])\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "a2482e2a-0306-4ff5-ba3b-9fb53051e019",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>19</td>\n",
       "      <td>3</td>\n",
       "      <td>22</td>\n",
       "      <td>33</td>\n",
       "      <td>44</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10</td>\n",
       "      <td>46</td>\n",
       "      <td>28</td>\n",
       "      <td>44</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>9</td>\n",
       "      <td>33</td>\n",
       "      <td>27</td>\n",
       "      <td>13</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>34</td>\n",
       "      <td>9</td>\n",
       "      <td>21</td>\n",
       "      <td>28</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>41</td>\n",
       "      <td>15</td>\n",
       "      <td>21</td>\n",
       "      <td>14</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D   E\n",
       "0  19   3  22  33  44\n",
       "1  10  46  28  44  40\n",
       "2   9  33  27  13  35\n",
       "3  34   9  21  28  17\n",
       "4  41  15  21  14  37"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.to_excel('salary1.xlsx',\n",
    "            sheet_name='salary', # 工作表名\n",
    "            header=True, # 导出列索引\n",
    "            index=False) # 不导出行索引\n",
    "\n",
    "# 读取 xlsx 文件\n",
    "res = pd.read_excel('salary1.xlsx',\n",
    "            sheet_name=0, #读取第一个工作表。也可以直接指定名字 'salary'\n",
    "            header=0, # 使用第一行数据作为列索引\n",
    "            names=list('ABCDE'), #替换列索引\n",
    "            # index_col=0 # 指定哪一列作为行索引。默认使用自然索引。如果导出了行索引，可设置为 0 使用它\n",
    "                   )\n",
    "\n",
    "res.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d95b9fe3-a0cd-4142-82b2-b2b1eedbc74f",
   "metadata": {},
   "source": [
    "**同时导出多个工作表**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "3d8c8178-88c6-4079-9c87-d566183082b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1 = pd.DataFrame(data = np.random.randint(0, 50, size = (50, 5)),\n",
    "               columns = ['IT', '化工', '生物', '教师', '土木'])\n",
    "df2 = pd.DataFrame(data = np.random.randint(0, 50, size = (150, 3)),\n",
    "               columns = ['Tensorflow', 'Pytorch', 'Keras'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "c2907668-0c7f-4d1a-b903-d875ac7a21fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "with pd.ExcelWriter('salary2.xlsx') as w:\n",
    "    df1.to_excel(w, sheet_name='salary', index=False)\n",
    "    df2.to_excel(w, sheet_name='score', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "67c76e1b-7826-4c96-abf9-787053f17f7d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Pytorch</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>8</td>\n",
       "      <td>30</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>18</td>\n",
       "      <td>21</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>27</td>\n",
       "      <td>31</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>16</td>\n",
       "      <td>48</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>17</td>\n",
       "      <td>11</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Tensorflow  Pytorch  Keras\n",
       "0           8       30      5\n",
       "1          18       21     29\n",
       "2          27       31      5\n",
       "3          16       48     29\n",
       "4          17       11     35"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3 = pd.read_excel('salary2.xlsx', sheet_name='score')\n",
    "df3.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "6987bcb8-2303-45fa-aa00-9724ca599305",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>IT</th>\n",
       "      <th>化工</th>\n",
       "      <th>生物</th>\n",
       "      <th>教师</th>\n",
       "      <th>土木</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>24</td>\n",
       "      <td>30</td>\n",
       "      <td>0</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>25</td>\n",
       "      <td>43</td>\n",
       "      <td>31</td>\n",
       "      <td>10</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>23</td>\n",
       "      <td>22</td>\n",
       "      <td>30</td>\n",
       "      <td>47</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>39</td>\n",
       "      <td>14</td>\n",
       "      <td>35</td>\n",
       "      <td>46</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>38</td>\n",
       "      <td>24</td>\n",
       "      <td>43</td>\n",
       "      <td>23</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   IT  化工  生物  教师  土木\n",
       "0  24  30   0  21  24\n",
       "1  25  43  31  10  35\n",
       "2  23  22  30  47   3\n",
       "3  39  14  35  46  11\n",
       "4  38  24  43  23  18"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4 = pd.read_excel('salary2.xlsx', sheet_name='salary')\n",
    "df4.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2aa4d24a-6b01-4604-b3cd-ddfde9bc8f30",
   "metadata": {},
   "source": [
    "### 其他格式\n",
    "\n",
    "sql、HDFS 等"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "549f52cf-05fa-4456-8fbe-51d09afb6343",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## 数据选择\n",
    "\n",
    "和 numpy 的花式索引类似"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c854e4cc-5fd6-4412-a968-5744106cad22",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>English</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>138</td>\n",
       "      <td>113</td>\n",
       "      <td>113</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>124</td>\n",
       "      <td>0</td>\n",
       "      <td>117</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>127</td>\n",
       "      <td>66</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>16</td>\n",
       "      <td>77</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>142</td>\n",
       "      <td>76</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  English  Math\n",
       "0     138      113   113\n",
       "1     124        0   117\n",
       "2     127       66    22\n",
       "3      16       77    54\n",
       "4     142       76    13"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.randint(0, 150, size=(1000, 3)),\n",
    "                  columns=['Python', 'English', 'Math'])\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ef692a8-f6e9-4f97-9d26-9443b5254008",
   "metadata": {},
   "source": [
    "**列选取**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "b395775b-23a8-4dd7-976d-9259a67b1df7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      138\n",
       "1      124\n",
       "2      127\n",
       "3       16\n",
       "4      142\n",
       "      ... \n",
       "995    109\n",
       "996      3\n",
       "997    132\n",
       "998     53\n",
       "999     52\n",
       "Name: Python, Length: 1000, dtype: int64"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取某一列，得到的数据类型为 Series\n",
    "df['Python']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "0c528ca3-5ca6-457f-82d8-c5c43fe10026",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      113\n",
       "1      117\n",
       "2       22\n",
       "3       54\n",
       "4       13\n",
       "      ... \n",
       "995     61\n",
       "996      1\n",
       "997     14\n",
       "998    133\n",
       "999    104\n",
       "Name: Math, Length: 1000, dtype: int64"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.Math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "7c9abf4f-d03a-4a0a-8766-34168f0e3e47",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>English</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>113</td>\n",
       "      <td>113</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>117</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>66</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>77</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>76</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>107</td>\n",
       "      <td>61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>106</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>77</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>71</td>\n",
       "      <td>133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>149</td>\n",
       "      <td>104</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     English  Math\n",
       "0        113   113\n",
       "1          0   117\n",
       "2         66    22\n",
       "3         77    54\n",
       "4         76    13\n",
       "..       ...   ...\n",
       "995      107    61\n",
       "996      106     1\n",
       "997       77    14\n",
       "998       71   133\n",
       "999      149   104\n",
       "\n",
       "[1000 rows x 2 columns]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#同时获取多列，得到的是 DataFrame 对象\n",
    "df[['English', 'Math']]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3dffbeb1-c7b3-4a39-a24b-0ad30705e9f2",
   "metadata": {},
   "source": [
    "**行选取**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "637cf4eb-0c80-48f6-9b73-2ec2252c499d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>English</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>98</td>\n",
       "      <td>49</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>41</td>\n",
       "      <td>0</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>111</td>\n",
       "      <td>54</td>\n",
       "      <td>103</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>65</td>\n",
       "      <td>94</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>133</td>\n",
       "      <td>96</td>\n",
       "      <td>127</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  English  Math\n",
       "A      98       49    15\n",
       "B      41        0    71\n",
       "C     111       54   103\n",
       "D      65       94     5\n",
       "E     133       96   127"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = pd.DataFrame(np.random.randint(0, 150, size=(5, 3)),\n",
    "                  columns=['Python', 'English', 'Math'],\n",
    "                  index=list('ABCDE'))\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "34ef82fa-5437-4736-b4d9-dba57fab566e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Python     98\n",
       "English    49\n",
       "Math       15\n",
       "Name: A, dtype: int64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取某一行\n",
    "df2.loc['A']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "482ff4ab-d146-4e5a-ac0b-a129e3471fb0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>English</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>98</td>\n",
       "      <td>49</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>65</td>\n",
       "      <td>94</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  English  Math\n",
       "A      98       49    15\n",
       "D      65       94     5"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取多行\n",
    "df2.loc[['A', 'D']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "3046593f-bb83-4212-9125-a787f4b92fbd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Python     98\n",
       "English    49\n",
       "Math       15\n",
       "Name: A, dtype: int64"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#通过整数来索引第一行\n",
    "df2.iloc[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "ec5a8c78-55a5-4ec7-83c3-bcf1cc483ca5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>English</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>98</td>\n",
       "      <td>49</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>41</td>\n",
       "      <td>0</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  English  Math\n",
       "A      98       49    15\n",
       "B      41        0    71"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#通过整数来索引多行\n",
    "df2.iloc[[0, 1]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90d9f412-fc3c-42d7-bed5-21a973a4213e",
   "metadata": {},
   "source": [
    "**获取具体坐标的数值**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "2e2ce90d-b3f9-4ff9-b411-704bba2e1cff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "71"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2['Math']['B']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "1965d380-898a-4936-92c6-3bcd42b698cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "71"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.loc['B']['Math']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "944f0d53-fd7c-4a5a-8225-5a5b5a74ab9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "71"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.loc['B', 'Math']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "2ec92bf7-725c-491a-9477-9cb6806ed3f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "71"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.loc['B'].Math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "b6c8a912-9f9e-4e1c-b9a9-1c69880df8dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "71\n",
      "71\n",
      "71\n"
     ]
    }
   ],
   "source": [
    "print(df2.iloc[1]['Math'])\n",
    "print(df2.iloc[1].Math)\n",
    "print(df2.iloc[1, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "0d362d75-16ff-4a9c-b206-3140c3cba572",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    49\n",
       "C    54\n",
       "Name: English, dtype: int64"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#指定多行的指定列\n",
    "df2.loc[['A','C']]['English']\n",
    "#df2.loc[['A','C'], 'English']\n",
    "#df2.loc[['A','C']].English"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "5e29b471-b14c-4b3f-ac07-9b4258f5381d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>English</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>49</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>54</td>\n",
       "      <td>103</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   English  Math\n",
       "A       49    15\n",
       "C       54   103"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#指定多行的指定多列\n",
    "df2.loc[['A','C']][['English', 'Math']]\n",
    "#df2.loc[['A','C'], ['English', 'Math']]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9370c7a1-16cc-4828-b152-951c4d2b989a",
   "metadata": {},
   "source": [
    "**切片**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "2bb1c0e6-362f-4175-a6df-4754d1388af0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>English</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>98</td>\n",
       "      <td>49</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>41</td>\n",
       "      <td>0</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>111</td>\n",
       "      <td>54</td>\n",
       "      <td>103</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  English  Math\n",
       "A      98       49    15\n",
       "B      41        0    71\n",
       "C     111       54   103"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.loc['A':'C']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "1da9e4c0-6539-4145-ab6f-462951eb2251",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>English</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>98</td>\n",
       "      <td>49</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>41</td>\n",
       "      <td>0</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>111</td>\n",
       "      <td>54</td>\n",
       "      <td>103</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  English  Math\n",
       "A      98       49    15\n",
       "B      41        0    71\n",
       "C     111       54   103"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.iloc[0:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "4c1fc2b9-484e-433f-a6bb-f325bd83c55b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>English</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>54</td>\n",
       "      <td>103</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>94</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>96</td>\n",
       "      <td>127</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   English  Math\n",
       "C       54   103\n",
       "D       94     5\n",
       "E       96   127"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#指定多行的指定多列\n",
    "#df2.loc['C':][['English', 'Math']]\n",
    "#df2.loc['C':, ['English', 'Math']]\n",
    "\n",
    "df2.iloc[2:][['English', 'Math']]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e0fcef5-788d-4fcf-aa43-f62509c1398a",
   "metadata": {},
   "source": [
    "**布尔索引**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "8321c5ca-0dfa-4e5f-9817-65fa4691e1fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      False\n",
       "1      False\n",
       "2      False\n",
       "3      False\n",
       "4      False\n",
       "       ...  \n",
       "995    False\n",
       "996    False\n",
       "997    False\n",
       "998    False\n",
       "999    False\n",
       "Name: Python, Length: 1000, dtype: bool"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.randint(0, 150, size=(1000, 3)),\n",
    "                  columns=['Python', 'English', 'Math'])\n",
    "\n",
    "# Python 零分的条件\n",
    "cond = df['Python'] == 0\n",
    "cond"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "6cd7a533-e9a1-4a78-a2ef-c292c21c9b16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>English</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>134</th>\n",
       "      <td>0</td>\n",
       "      <td>107</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>210</th>\n",
       "      <td>0</td>\n",
       "      <td>62</td>\n",
       "      <td>125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>301</th>\n",
       "      <td>0</td>\n",
       "      <td>110</td>\n",
       "      <td>59</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>645</th>\n",
       "      <td>0</td>\n",
       "      <td>55</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>831</th>\n",
       "      <td>0</td>\n",
       "      <td>117</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>884</th>\n",
       "      <td>0</td>\n",
       "      <td>28</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     Python  English  Math\n",
       "134       0      107    95\n",
       "210       0       62   125\n",
       "301       0      110    59\n",
       "645       0       55    32\n",
       "831       0      117    84\n",
       "884       0       28    80"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[cond]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "dd884cfc-3074-4ed1-9544-28e6df12d622",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>English</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>204</th>\n",
       "      <td>145</td>\n",
       "      <td>143</td>\n",
       "      <td>148</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     Python  English  Math\n",
       "204     145      143   148"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond1 = df['Python'] > 140\n",
    "cond2 = df['Math'] > 140\n",
    "cond3 = df['English'] > 140\n",
    "cond = cond1 & cond2 & cond3\n",
    "df[cond]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee46df42-1c33-4c37-b485-cc542298573f",
   "metadata": {},
   "source": [
    "**赋值操作**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "f1f576d3-8984-41d6-8656-7905128cdb8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pandas</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>45</td>\n",
       "      <td>115</td>\n",
       "      <td>101</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>95</td>\n",
       "      <td>25</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>54</td>\n",
       "      <td>68</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>107</td>\n",
       "      <td>43</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>116</td>\n",
       "      <td>5</td>\n",
       "      <td>98</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Pandas  Tensorflow  Keras\n",
       "A      45         115    101\n",
       "B      95          25     91\n",
       "C      54          68     32\n",
       "D     107          43     91\n",
       "E     116           5     98"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#一维序列，表示 10 个学生的 Pytorch 成绩\n",
    "s = pd.Series(np.random.randint(0, 150, size=10),\n",
    "              index=list('ABCDEFGHIJ'),\n",
    "              name='Pytorch')\n",
    "\n",
    "#二维数组，表示学生成绩\n",
    "df = pd.DataFrame(np.random.randint(0, 150, size=(10, 3)),\n",
    "                  index=list('ABCDEFGHIJ'),\n",
    "                  columns=['Pandas', 'Tensorflow', 'Keras'])\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "d895e7ff-c7b0-4e8f-b4ef-ed8c16f3c094",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pandas</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "      <th>Java</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>45</td>\n",
       "      <td>115</td>\n",
       "      <td>101</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>95</td>\n",
       "      <td>25</td>\n",
       "      <td>91</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>54</td>\n",
       "      <td>68</td>\n",
       "      <td>32</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>107</td>\n",
       "      <td>43</td>\n",
       "      <td>91</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>116</td>\n",
       "      <td>5</td>\n",
       "      <td>98</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Pandas  Tensorflow  Keras  Java\n",
       "A      45         115    101    55\n",
       "B      95          25     91    37\n",
       "C      54          68     32    30\n",
       "D     107          43     91    17\n",
       "E     116           5     98    91"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['Java'] = np.random.randint(0, 100, size = 10)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "c61783ff-0795-43a9-bc16-3513bbf80c1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pandas</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "      <th>Java</th>\n",
       "      <th>Pytorch</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>45</td>\n",
       "      <td>115</td>\n",
       "      <td>101</td>\n",
       "      <td>55</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>95</td>\n",
       "      <td>25</td>\n",
       "      <td>91</td>\n",
       "      <td>37</td>\n",
       "      <td>135</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>54</td>\n",
       "      <td>68</td>\n",
       "      <td>32</td>\n",
       "      <td>30</td>\n",
       "      <td>104</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>107</td>\n",
       "      <td>43</td>\n",
       "      <td>91</td>\n",
       "      <td>17</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>116</td>\n",
       "      <td>5</td>\n",
       "      <td>98</td>\n",
       "      <td>91</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Pandas  Tensorflow  Keras  Java  Pytorch\n",
       "A      45         115    101    55       66\n",
       "B      95          25     91    37      135\n",
       "C      54          68     32    30      104\n",
       "D     107          43     91    17       58\n",
       "E     116           5     98    91       91"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#增加一列，会根据行索引自动对齐\n",
    "df['Pytorch'] = s\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "b879a50f-a483-4e2d-ad15-bcee067d0256",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pandas</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "      <th>Java</th>\n",
       "      <th>Pytorch</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>45</td>\n",
       "      <td>115</td>\n",
       "      <td>101</td>\n",
       "      <td>55</td>\n",
       "      <td>150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>95</td>\n",
       "      <td>25</td>\n",
       "      <td>150</td>\n",
       "      <td>37</td>\n",
       "      <td>135</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>54</td>\n",
       "      <td>68</td>\n",
       "      <td>32</td>\n",
       "      <td>100</td>\n",
       "      <td>104</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>107</td>\n",
       "      <td>43</td>\n",
       "      <td>91</td>\n",
       "      <td>100</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>116</td>\n",
       "      <td>5</td>\n",
       "      <td>98</td>\n",
       "      <td>91</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Pandas  Tensorflow  Keras  Java  Pytorch\n",
       "A      45         115    101    55      150\n",
       "B      95          25    150    37      135\n",
       "C      54          68     32   100      104\n",
       "D     107          43     91   100       58\n",
       "E     116           5     98    91       91"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#按标签赋值\n",
    "df.loc['A', 'Pytorch'] = 150\n",
    "\n",
    "#按位置赋值\n",
    "df.iloc[1, 2] = 150\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "8804605f-f930-4b32-81fe-0556f5fcae30",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pandas</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "      <th>Java</th>\n",
       "      <th>Pytorch</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>100</td>\n",
       "      <td>115</td>\n",
       "      <td>101</td>\n",
       "      <td>55</td>\n",
       "      <td>150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>100</td>\n",
       "      <td>25</td>\n",
       "      <td>150</td>\n",
       "      <td>37</td>\n",
       "      <td>135</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>100</td>\n",
       "      <td>68</td>\n",
       "      <td>32</td>\n",
       "      <td>100</td>\n",
       "      <td>104</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>100</td>\n",
       "      <td>43</td>\n",
       "      <td>91</td>\n",
       "      <td>100</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>100</td>\n",
       "      <td>5</td>\n",
       "      <td>98</td>\n",
       "      <td>91</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Pandas  Tensorflow  Keras  Java  Pytorch\n",
       "A     100         115    101    55      150\n",
       "B     100          25    150    37      135\n",
       "C     100          68     32   100      104\n",
       "D     100          43     91   100       58\n",
       "E     100           5     98    91       91"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#对某一列进行赋值\n",
    "#df.loc[:, 'Pandas'] = 10\n",
    "df.loc[:, 'Pandas'] = np.array([100] * 10)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "1c01048f-dfe1-4af3-9ec3-34be6707ff93",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pandas</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "      <th>Java</th>\n",
       "      <th>Pytorch</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>92</td>\n",
       "      <td>92</td>\n",
       "      <td>92</td>\n",
       "      <td>92</td>\n",
       "      <td>92</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>100</td>\n",
       "      <td>40</td>\n",
       "      <td>150</td>\n",
       "      <td>69</td>\n",
       "      <td>115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>100</td>\n",
       "      <td>109</td>\n",
       "      <td>60</td>\n",
       "      <td>66</td>\n",
       "      <td>96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>100</td>\n",
       "      <td>68</td>\n",
       "      <td>79</td>\n",
       "      <td>75</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>100</td>\n",
       "      <td>127</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>104</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Pandas  Tensorflow  Keras  Java  Pytorch\n",
       "A      92          92     92    92       92\n",
       "B     100          40    150    69      115\n",
       "C     100         109     60    66       96\n",
       "D     100          68     79    75       14\n",
       "E     100         127      4     1      104"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#对某一行进行赋值\n",
    "#df.iloc[0] = 92\n",
    "df.iloc[0] = np.array([92] * 5)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "2f4e911f-54d6-4015-a097-657e45763aaf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pandas</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "      <th>Java</th>\n",
       "      <th>Pytorch</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>92</td>\n",
       "      <td>92</td>\n",
       "      <td>92</td>\n",
       "      <td>92</td>\n",
       "      <td>92</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>100</td>\n",
       "      <td>92</td>\n",
       "      <td>150</td>\n",
       "      <td>92</td>\n",
       "      <td>115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>100</td>\n",
       "      <td>109</td>\n",
       "      <td>92</td>\n",
       "      <td>92</td>\n",
       "      <td>96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>100</td>\n",
       "      <td>92</td>\n",
       "      <td>92</td>\n",
       "      <td>92</td>\n",
       "      <td>92</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>100</td>\n",
       "      <td>127</td>\n",
       "      <td>92</td>\n",
       "      <td>92</td>\n",
       "      <td>104</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Pandas  Tensorflow  Keras  Java  Pytorch\n",
       "A      92          92     92    92       92\n",
       "B     100          92    150    92      115\n",
       "C     100         109     92    92       96\n",
       "D     100          92     92    92       92\n",
       "E     100         127     92    92      104"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond = df < 92\n",
    "df[cond] = 92\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7dbedc5-a73f-4a24-a8a8-e312fe185606",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## 练习 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97aa2f1c-3d0c-4a39-8255-f843885d7684",
   "metadata": {},
   "source": [
    "1\n",
    "\n",
    "创建 1000 条语文、数学、英语、Python 的考试成绩 DataFrame，最高分为 150，分别将数据保存到 csv 文件和 excel 文件，保存时不保存行索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "a950970c-bcd3-492a-8b5e-2307ea57304a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>Python</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>64</td>\n",
       "      <td>96</td>\n",
       "      <td>30</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>22</td>\n",
       "      <td>79</td>\n",
       "      <td>140</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10</td>\n",
       "      <td>86</td>\n",
       "      <td>129</td>\n",
       "      <td>149</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>41</td>\n",
       "      <td>80</td>\n",
       "      <td>57</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>79</td>\n",
       "      <td>112</td>\n",
       "      <td>91</td>\n",
       "      <td>141</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   语文   数学   英语  Python\n",
       "0  64   96   30      54\n",
       "1  22   79  140      12\n",
       "2  10   86  129     149\n",
       "3  41   80   57      35\n",
       "4  79  112   91     141"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.randint(0, 151, size=(1000, 4)),\n",
    "                  columns=['语文', '数学', '英语', 'Python'])\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "c9601be6-5408-4ab0-830a-e773ec058147",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv('scores.csv', index=False, header=True)\n",
    "df.to_excel('scores.xlsx', index=False, header=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f4acaed-1b17-4415-922c-33be56745a4e",
   "metadata": {},
   "source": [
    "2\n",
    "\n",
    "使用字典创建 DataFrame，行索引为 a\\~z，列索引是身高（160\\~185）、体重（50\\~90）、学历（无、本科、硕士、博士）\n",
    "身高和体重使用 Numpy 随机生成，学历数据先创建数组 `edu=np.array([\"无\",\"本科\",\"硕士\",\"博士\"])`，然后使用花式索引从四个数据中选择 26 个数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "8420036e-6cf4-461a-b952-a8bdd53b8333",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['硕士', '博士', '无', '本科', '硕士', '无', '博士', '无', '博士', '硕士', '无', '本科',\n",
       "       '无', '本科', '博士', '无', '博士', '本科', '本科', '硕士', '硕士', '本科', '本科',\n",
       "       '硕士', '硕士', '博士'], dtype='<U2')"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "edu = np.array([\"无\",\"本科\",\"硕士\",\"博士\"])\n",
    "random_idx = np.random.randint(0, 4, size = 26)\n",
    "edu[random_idx]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "a23ccbd8-faaa-4a72-abc4-3a85aa8c8ea2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>height</th>\n",
       "      <th>weight</th>\n",
       "      <th>education</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>175</td>\n",
       "      <td>60</td>\n",
       "      <td>硕士</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>167</td>\n",
       "      <td>68</td>\n",
       "      <td>博士</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>171</td>\n",
       "      <td>90</td>\n",
       "      <td>无</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>164</td>\n",
       "      <td>57</td>\n",
       "      <td>本科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>165</td>\n",
       "      <td>73</td>\n",
       "      <td>硕士</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   height  weight education\n",
       "a     175      60        硕士\n",
       "b     167      68        博士\n",
       "c     171      90         无\n",
       "d     164      57        本科\n",
       "e     165      73        硕士"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import string\n",
    "\n",
    "df = pd.DataFrame(data = {\n",
    "                    'height': np.random.randint(160, 186, size=26),\n",
    "                    'weight': np.random.randint(50, 91, size=26),\n",
    "                    'education': edu[random_idx],\n",
    "                    }, \n",
    "                  index=[letter for letter in string.ascii_lowercase])\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5b2f39a-eb38-4c3d-9575-657a3e3da0a8",
   "metadata": {},
   "source": [
    "3\n",
    "\n",
    "使用题目 2 中的数据进行筛选：\n",
    "1) 筛选索引大于 't' 的所有数据\n",
    "2) 筛选学历为博士，身高大于 170 或体重小于 80 的学生"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "d8471482-f03d-48a5-9ca0-7c92fdba28c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>height</th>\n",
       "      <th>weight</th>\n",
       "      <th>education</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>t</th>\n",
       "      <td>175</td>\n",
       "      <td>79</td>\n",
       "      <td>硕士</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>u</th>\n",
       "      <td>160</td>\n",
       "      <td>65</td>\n",
       "      <td>硕士</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>v</th>\n",
       "      <td>171</td>\n",
       "      <td>76</td>\n",
       "      <td>本科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>w</th>\n",
       "      <td>166</td>\n",
       "      <td>70</td>\n",
       "      <td>本科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>179</td>\n",
       "      <td>86</td>\n",
       "      <td>硕士</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>y</th>\n",
       "      <td>177</td>\n",
       "      <td>59</td>\n",
       "      <td>硕士</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>z</th>\n",
       "      <td>185</td>\n",
       "      <td>87</td>\n",
       "      <td>博士</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   height  weight education\n",
       "t     175      79        硕士\n",
       "u     160      65        硕士\n",
       "v     171      76        本科\n",
       "w     166      70        本科\n",
       "x     179      86        硕士\n",
       "y     177      59        硕士\n",
       "z     185      87        博士"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['t':]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "becadaa2-5b07-4249-aca0-d81a9b155b39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>height</th>\n",
       "      <th>weight</th>\n",
       "      <th>education</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>g</th>\n",
       "      <td>184</td>\n",
       "      <td>62</td>\n",
       "      <td>博士</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   height  weight education\n",
       "g     184      62        博士"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond1 = df['education'] == '博士'\n",
    "cond2 = df['weight'] < 80\n",
    "cond3 = df['height'] > 170\n",
    "cond = cond1 & cond2 & cond3\n",
    "df[cond]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd02bdbd-9438-4e82-92e8-a415714554a6",
   "metadata": {},
   "source": [
    "4\n",
    "\n",
    "对题目 2 中的数据进行修改\n",
    "1) 本科生减肥，减掉体重 10\n",
    "2) 博士生减肥，减掉体重范围是 5~10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "fe110038-1041-4b86-9f31-fc43295a3243",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>height</th>\n",
       "      <th>weight</th>\n",
       "      <th>education</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>164</td>\n",
       "      <td>57</td>\n",
       "      <td>本科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>l</th>\n",
       "      <td>172</td>\n",
       "      <td>90</td>\n",
       "      <td>本科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>n</th>\n",
       "      <td>185</td>\n",
       "      <td>82</td>\n",
       "      <td>本科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>r</th>\n",
       "      <td>169</td>\n",
       "      <td>68</td>\n",
       "      <td>本科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>s</th>\n",
       "      <td>174</td>\n",
       "      <td>83</td>\n",
       "      <td>本科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>v</th>\n",
       "      <td>171</td>\n",
       "      <td>76</td>\n",
       "      <td>本科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>w</th>\n",
       "      <td>166</td>\n",
       "      <td>70</td>\n",
       "      <td>本科</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   height  weight education\n",
       "d     164      57        本科\n",
       "l     172      90        本科\n",
       "n     185      82        本科\n",
       "r     169      68        本科\n",
       "s     174      83        本科\n",
       "v     171      76        本科\n",
       "w     166      70        本科"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond = df['education'] == '本科'\n",
    "df.loc[cond]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "135a26c5-5fc5-47b6-a7b6-e813c6c7acc8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>height</th>\n",
       "      <th>weight</th>\n",
       "      <th>education</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>164</td>\n",
       "      <td>47</td>\n",
       "      <td>本科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>l</th>\n",
       "      <td>172</td>\n",
       "      <td>80</td>\n",
       "      <td>本科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>n</th>\n",
       "      <td>185</td>\n",
       "      <td>72</td>\n",
       "      <td>本科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>r</th>\n",
       "      <td>169</td>\n",
       "      <td>58</td>\n",
       "      <td>本科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>s</th>\n",
       "      <td>174</td>\n",
       "      <td>73</td>\n",
       "      <td>本科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>v</th>\n",
       "      <td>171</td>\n",
       "      <td>66</td>\n",
       "      <td>本科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>w</th>\n",
       "      <td>166</td>\n",
       "      <td>60</td>\n",
       "      <td>本科</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   height  weight education\n",
       "d     164      47        本科\n",
       "l     172      80        本科\n",
       "n     185      72        本科\n",
       "r     169      58        本科\n",
       "s     174      73        本科\n",
       "v     171      66        本科\n",
       "w     166      60        本科"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[cond, 'weight'] -= 10\n",
    "df.loc[cond]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "dea95d7d-6971-442d-bf27-a45f9b9bbe64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>height</th>\n",
       "      <th>weight</th>\n",
       "      <th>education</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>167</td>\n",
       "      <td>68</td>\n",
       "      <td>博士</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>g</th>\n",
       "      <td>184</td>\n",
       "      <td>62</td>\n",
       "      <td>博士</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>i</th>\n",
       "      <td>169</td>\n",
       "      <td>80</td>\n",
       "      <td>博士</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>o</th>\n",
       "      <td>168</td>\n",
       "      <td>64</td>\n",
       "      <td>博士</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>q</th>\n",
       "      <td>167</td>\n",
       "      <td>59</td>\n",
       "      <td>博士</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>z</th>\n",
       "      <td>185</td>\n",
       "      <td>87</td>\n",
       "      <td>博士</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   height  weight education\n",
       "b     167      68        博士\n",
       "g     184      62        博士\n",
       "i     169      80        博士\n",
       "o     168      64        博士\n",
       "q     167      59        博士\n",
       "z     185      87        博士"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond = df['education'] == '博士'\n",
    "df.loc[cond]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "9903d2c5-ee48-44a8-87f4-64a0de139138",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[cond].shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "a0534cc9-5f14-464a-93e5-d77092622c3a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 8,  6, 10,  7,  6,  7])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(5, 11, size=df[cond].shape[0])\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "198e74a3-acee-4dab-acdf-bfc56606e83d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#两个数组相减\n",
    "df.loc[cond, 'weight'] -= arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "5db40e6a-bfb8-41db-8b82-49a345649a73",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>height</th>\n",
       "      <th>weight</th>\n",
       "      <th>education</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>167</td>\n",
       "      <td>60</td>\n",
       "      <td>博士</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>g</th>\n",
       "      <td>184</td>\n",
       "      <td>56</td>\n",
       "      <td>博士</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>i</th>\n",
       "      <td>169</td>\n",
       "      <td>70</td>\n",
       "      <td>博士</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>o</th>\n",
       "      <td>168</td>\n",
       "      <td>57</td>\n",
       "      <td>博士</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>q</th>\n",
       "      <td>167</td>\n",
       "      <td>53</td>\n",
       "      <td>博士</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>z</th>\n",
       "      <td>185</td>\n",
       "      <td>80</td>\n",
       "      <td>博士</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   height  weight education\n",
       "b     167      60        博士\n",
       "g     184      56        博士\n",
       "i     169      70        博士\n",
       "o     168      57        博士\n",
       "q     167      53        博士\n",
       "z     185      80        博士"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[cond]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a5adcc9-5cd5-4ffa-b3de-5a32ab65994f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "945d2c04-c862-4346-a815-09d00f6824e3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
